
//加载列表
(function($){
    var cleanList = function(obj){
    	$(obj).hide();
    	do{
    		if($(obj).prev() == null) {
    			return;
    		} else {
    			var data = $(obj).prev().attr("data");
        		if(data != 'loadList'){
        			return;
        		} else {
        			$(obj).prev().remove();
        		}
    		}
    	}while(true);
    }
    var loadData = function(obj, opt){
    	var htmltemp = $(obj).html();
    	htmltemp = $.loadData({html:htmltemp,data:opt});
    	var htmlobj = $(htmltemp);
    	htmlobj.attr("data","loadList");
    	$(obj).clean();
    	$(obj).before(htmlobj);
    }
    var addData = function(obj, opt){
    	var htmltemp = $(obj).html();
    	htmltemp = $.loadData({html:htmltemp,data:opt});
    	var htmlobj = $(htmltemp);
    	htmlobj.attr("data","loadList");
    	$(obj).before(htmlobj);
    }
    var options= {
    }
    $.fn.extend({
        clean:function(opt){
            this.each(function(){
            	cleanList(this, opt);
            });
        },
        loadData:function(opt){
        	this.each(function(){
        		loadData(this, opt);
            });
        },
        loadAddData:function(opt){
        	this.each(function(){
        		addData(this, opt);
            });
        }
    });
})(jQuery);


(function($){
	var replaceHtml = function(html, data){
		
		var ts = html.match(/#\{(\S*)(?=\}#)/g);
        if(ts != null){
        	$.each(ts, function(i,v){
            	var k = v.replace("#{","");
            	var value = hsHanldGet(data, k);
            	html = html.replace(v+"}#",value);
            });
        }
        return html;
	}
	
	var hsHanldGet=function(data, key){
		if(data == null){
			return "";
		}
		var k1 = '';
		var bIndex = key.indexOf(".");
		if(bIndex == -1){
			k1 = key;
		} else {
			k1 = key.substring(0, bIndex);
			var k2 = key.substring(bIndex+1, key.length);
			return hsHanldGet(data[k1], k2);
		}
		return (data[k1]==null?"":data[k1]);
	}
	
	var searData = function(data, all, key){
		var d;
		var bIndex = key.indexOf(".");
		var k1;
		if(bIndex != -1){
			k1 = key.substring(0, bIndex);
		} else {
			k1 = key;
		}
		if(data[k1] != null){
			d = data;
		} else if(all != null && all[k1] != null){
			d = all;
		}
		return d;
	}
	
	var hsHanld = function(value, bm, data, all){
		var hxs = [">=", "<=", "!=", "==", "<", ">"];
		var flag = false;
		$.each(hxs, function(i,hs){
			if(value.indexOf(hs)!= -1){
				var k = value.split(hs)[0];
				if(bm != null && bm != ''){
	    			k = k.replace(bm+".", "");
				}
				var v = value.split(hs)[1];
				
				if(v.substring(0,1) == "'" && v.substring(v.length-1, v.length) == "'"){
					val2 = v.substring(1, v.length-1);
				} else {
					if(bm != null && bm != ''){
		    			v = v.replace(bm+".", "");
					}
					var d2 = searData(data, all ,v);
					val2 = hsHanldGet(d2, v);
				}
				
				var d = searData(data, all ,k);
				var val1 = hsHanldGet(d, k);
				
				
				if((hs == '>=' && val1 >= val2) || (hs == '<=' && val1 <= val2) 
						|| (hs == '!=' && val1 != val2) || (hs == '=' && val1 == val2) 
						|| (hs == '==' && val1 == val2) || (hs == '>' && val1 > val2) || (hs == '<' && val1 < val2)){
					flag = true;
				}
				return 2;
			}
		});
		return flag;
	}
	
	
	var getIfSonHtml = function(html, bIndex, eIndex, i){
		var tempEnd = "#endif#";
		var tempHtml = html.substring(bIndex, eIndex);
		var iftemps = tempHtml.match(/#if\((.*)(?=\)#)/g);
		if(iftemps != null){
			if(i == iftemps.length){
				return eIndex;
			}
			eIndex = html.indexOf(tempEnd, eIndex+tempEnd.length);
			i++;
			return getIfSonHtml(html, bIndex, eIndex, i);
		}
	}
	
	var replaceIf=function(tempHtml, data, bm, all){
		bm = (bm==null?'':bm)
    	var iftemps = tempHtml.match(/#if\((.*)(?=\)#)/g);
    	if(iftemps != null){
	    	$.each(iftemps, function(i,tempv){
	    		var tempbegin = tempv+")#"
	        	var tempEnd = "#endif#";
	    		var value = tempv.replace("#if(", "");
	    		var bIndexOf = 0;
	    		while(bIndexOf != -1){
	    			var htmls = '';
	    			bIndexOf = tempHtml.indexOf(tempbegin, bIndexOf);
	    			if(bIndexOf != -1){
	    				var eIndexOf = tempHtml.indexOf(tempEnd, bIndexOf);
	    				eIndexOf = getIfSonHtml(tempHtml, bIndexOf, eIndexOf, 1);
	    				var html = tempHtml.substring(bIndexOf+tempbegin.length, eIndexOf);
	    				var flag = false;
	    				if(value.indexOf("&&") != -1){
	    					flag = true;
	    					var valueifs = value.split("&&");
	    					$.each(valueifs, function(i,valueif){
	    						var f = hsHanld(valueif.trim(), bm, data, all);
	    						if(!f){
	    							flag = false;
	    							return;
	    						}
	    					});
	    				} else if(value.indexOf("||") != -1){
	    					flag = false;
	    					var valueifs = value.split("||");
	    					$.each(valueifs, function(i,valueif){
	    						var f = hsHanld(valueif.trim(), bm, data, all);
	    						if(f){
	    							flag = true;
	    							return;
	    						}
	    					});
	    				} else {
	    					flag = hsHanld(value, bm, data, all);
	    				}
	    				
	    				if(flag){
	    					htmls = html+'';
	    				}
	    	    		tempHtml = tempHtml.substring(0, bIndexOf)+ htmls +tempHtml.substring(eIndexOf+tempEnd.length, tempHtml.length);
	    			}
	    			
	    		}
	    	});
    	}
    	return tempHtml;
	}
	
	var getForSonHtml = function(html, bIndex, eIndex, i){
		var tempEnd = "#endfor#";
		var tempHtml = html.substring(bIndex, eIndex);
		var iftemps = tempHtml.match(/#for\((\S*)(?=\)#)/g);
		if(iftemps != null){
			if(i == iftemps.length){
				return eIndex;
			}
			eIndex = html.indexOf(tempEnd, eIndex+tempEnd.length);
			i++;
			return getForSonHtml(html, bIndex, eIndex, i);
		}
	}
	
	var replaceFor=function(tempHtml, data, bm2, all){
		var temps = tempHtml.match(/#for\((\S*)(?=\)#)/g);
    	var newHtml = tempHtml+'';
    	if(temps != null){
    		newHtml = '';
	    	$.each(temps, function(i,v){
	    		var tempbegin = v+")#"
	        	var tempEnd = "#endfor#";
	    		var bIndexOf = 0;
	    		while(bIndexOf != -1){
	    			bIndexOf = tempHtml.indexOf(tempbegin, bIndexOf);
	    			if(bIndexOf != -1){
	    				
	    				var eIndexOf = tempHtml.indexOf(tempEnd, bIndexOf);
	    				eIndexOf = getForSonHtml(tempHtml, bIndexOf, eIndexOf, 1);
	    				var html = tempHtml.substring(bIndexOf+tempbegin.length, eIndexOf);
	    				var value = v.replace("#for(", "");
	    	    		var key = value.split(":")[1];
	    	    		if(bm2 != null && bm2 != ''){
	    	    			key = key.replace(bm2+".", "");
	    				}
	    	    		var bm = value.split(":")[0];
	    	    		
	    	    		
	    	    		var newHtml = '';
	    	    		if(data != null && data[key] != null){
	    	    			var ts = html.match(/#\{(\S*)(?=\}#)/g);
	    	    			$.each(data[key], function(i3,v3){
	    	    				var ht = html+"";
	    	    				ht = replaceFor(ht, v3, bm, all);
	    		    			ht = replaceIf(ht, v3, bm ,all);
	    		    			$.each(ts, function(i2,v2){
	    		    				var k = v2.replace("#{", "");
	    		        			if(k == bm){
	    		        				var value = (v3==null?"":v3);
	    		        				ht= ht.replace(v2+"}#", value);
	    		        			} else {
	    		        				k = k.replace(bm+".","");
	    		        				var value = hsHanldGet(v3, k);
	    		        				ht= ht.replace(v2+"}#", value);
	    		        			}
	    		        			
	    		        		});
	    		    			newHtml+=ht;
	    		    		});
	    	    		} else if(all != null && all[key] != null){
	    	    			var ts = html.match(/#\{(\S*)(?=\}#)/g);
	    	    			$.each(all[key], function(i3,v3){
	    	    				var ht = html+"";
	    	    				ht = replaceFor(ht, v3, bm, all);
	    		    			ht = replaceIf(ht, v3, bm ,all);
	    		    			$.each(ts, function(i2,v2){
	    		    				var k = v2.replace("#{", "");
	    		        			if(k == bm){
	    		        				ht= (v3==null?"":v3);
	    		        			} else {
	    		        				k = k.replace(bm+".","");
	    		        				var value = hsHanldGet(v3, k);
	    		        				ht= ht.replace(v2+"}#", value);
	    		        			}
	    		        		});
	    		    			
	    		    			newHtml+=ht;
	    		    		});
	    	    		}
	    	    		tempHtml = tempHtml.substring(0, bIndexOf)+ newHtml +tempHtml.substring(eIndexOf+tempEnd.length, tempHtml.length-1);
	    			}
	    		}
	    	});
	    	newHtml = newHtml+tempHtml;
    	}
    	return newHtml;
	}
	
    var main = function(opt){
    	var newHtml = opt.html;
    	newHtml = replaceFor(newHtml, opt.data, '', opt.data);
    	
    	newHtml = replaceIf(newHtml, opt.data);
    	
    	if(newHtml == ''){
    		newHtml = opt.html;
    	}
    	newHtml = replaceHtml(newHtml, opt.data);
    	return newHtml;
    }
    var options = {
        html:"",
        data : {}
    };

    $.extend({
        loadData:function(opt) {
            var opt2 = $.extend({}, options, opt||{});
            return main(opt2);
        }
    });
})(jQuery);



(function($){
	var ajaxData = function(obj, opt){
    	var data;
    	var async = false;
    	if(opt.success != null) {
    		async = true;
    	}
    	if(opt.isMask){
    		$("body").append('<div id="dialog-mask" style="overflow: hidden;z-index: 1150;width: 100%;height: 100%;background-color: #fff;opacity: .3;position: fixed;top: 0px;left: 0px;"><i class="icon-spinner icon-spin icon-2x" style="top:50%;left:50%;position: absolute;color: #fff;font-size: 30px;"></i></div>');	
    	}
    	
    	$.ajax({async:async, url:opt.url,type:opt.type,dataType:"json",headers:opt.headers,cache:false,data:opt.data, success:function(obj){
    		$("#dialog-mask").remove();
			if(opt.success != null){
    			opt.success(obj);
    		}
    	},error:function(obj){
    		$("#dialog-mask").remove();
    		if(opt.error == null){
    			$.jjalert({title:"警告",text:"系统异常"});
    		} else {
    			opt.error();
    		}
    		
    	}});
    	return data;
    }
    var options = {
    	isMask:true,
    	url:"",
    	type:"get",
    	success:null,
    	error:null,
    	data:{}
    };
    $.extend({
    	ajaxData:function(opt) {
    		var opt2 = $.extend({}, options, opt||{});
        	return ajaxData(this, opt2);
        }
    });
})(jQuery);

